from marshmallow import fields, post_load

from gis.common.django_ext.forms import EnumField
from itpay.core.agency.const import PayType
from itpay.server_api.view.base import MchSignRequestSchema


class AddOrderSchema(MchSignRequestSchema):
    user_id = fields.String(required=True)
    recharge_amount = fields.Integer(required=True)
    pay_type = EnumField(PayType, required=True)
    remark = fields.String(allow_none=True)
    account_num = fields.String(required=True)
    account_holder = fields.String(required=True)
    recharge_success_amount = fields.Integer(allow_none=True)

    @post_load
    def post_load_func(self, data):
        if data.get("account_num"):
            assert data.get("account_holder")
            data["pay_info"] = dict(
                account_num=data["account_num"], account_holder=data["account_holder"]
            )
            del data["account_num"]
            del data["account_holder"]
        return data
